package net.arvin.mobilebanksystem.fragment;

import net.arvin.dao.ResponseHandler;
import net.arvin.mobilebanksystem.R;
import net.arvin.mobilebanksystem.activity.MainActivity;
import net.arvin.mobilebanksystem.activity.UserInfoActivity;
import net.arvin.service.UserService;
import net.arvin.utils.NetConnectionUtil;
import net.arvin.utils.PreferenceUtils;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginFragment extends Fragment implements OnClickListener {
	public EditText phonenumber;
	public EditText password;
	public Button loginBtn;
	public Button registerBtn;
	public TextView findPSW;

	private ResponseHandler mResponseHandler;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_login, null);
		init(view);
		return view;
	}

	private void init(View view) {
		initView(view);
		initListenner();
		updateData();
	}

	private void updateData() {
		String phoneNum = PreferenceUtils.getPreferPhonenumber(getActivity());
		if (phoneNum != null) {
			phonenumber.setText(phoneNum);
			phonenumber.setSelection(phoneNum.length());
		}
	}

	private void initListenner() {
		loginBtn.setOnClickListener(this);
		registerBtn.setOnClickListener(this);
		findPSW.setOnClickListener(this);
	}

	private void initView(View view) {
		phonenumber = (EditText) view.findViewById(R.id.phonenumber);
		password = (EditText) view.findViewById(R.id.password);
		loginBtn = (Button) view.findViewById(R.id.login);
		registerBtn = (Button) view.findViewById(R.id.register);
		findPSW = (TextView) view.findViewById(R.id.findPSW);
	}

	private void loginValidate(String phonenumber, String password) {
		if (phonenumber == null || phonenumber.trim().equals("")) {
			Toast.makeText(getActivity(), "手机号不能为空~", Toast.LENGTH_SHORT)
					.show();
			return;
		}
		if (phonenumber.trim().length() != 11) {
			Toast.makeText(getActivity(), "手机号长度不对~", Toast.LENGTH_SHORT)
					.show();
			return;
		}
		if (password == null || password.trim().equals("")) {
			Toast.makeText(getActivity(), "密码不能为空~", Toast.LENGTH_SHORT).show();
			return;
		}
		if (NetConnectionUtil.isConnectWithTip(getActivity(), R.string.notConnectNet)) {
			UserService.tryLogin(phonenumber, password, getResponseHandler(),
					getActivity());
		}
	}

	private void startMainActivity() {
		Intent intent = new Intent();
		intent.setClass(getActivity(), MainActivity.class);
		getActivity().finish();
		startActivity(intent);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.login:
			loginValidate(phonenumber.getText().toString(), password.getText()
					.toString());
			break;
		case R.id.register:
			startRegisterFragment();
			break;
		case R.id.findPSW:
			startRetrievePSWFragment();
			break;
		}
	}

	private void startRetrievePSWFragment() {
		UserInfoActivity.userinfoActivity.showTitleBar();
		UserInfoActivity.userinfoActivity.setTitleTv("验证手机号");
		UserInfoActivity.userinfoActivity
				.switchContent(new ResetPSWFragment());
	}

	private void startRegisterFragment() {
		UserInfoActivity.userinfoActivity.showTitleBar();
		UserInfoActivity.userinfoActivity.setTitleTv("注册");
		UserInfoActivity.userinfoActivity.switchContent(new RegisterFragment());
	}

	private ResponseHandler getResponseHandler() {
		if (mResponseHandler == null) {
			mResponseHandler = new ResponseHandler() {
				@Override
				public void onRequsetSucceeded(final Object data) {
					PreferenceUtils.modifyStringValueInPreferences(
							PreferenceUtils.User, data.toString(),
							getActivity());
					PreferenceUtils.modifyStringValueInPreferences(
							PreferenceUtils.Token, "token", getActivity());
					PreferenceUtils.modifyStringValueInPreferences(
							PreferenceUtils.Phonenumber, phonenumber.getText()
									.toString(), getActivity());
					startMainActivity();
				}

				@Override
				public void onRequestFailed(final Object data) {
					Toast.makeText(getActivity(), "用户名密码不匹配!",
							Toast.LENGTH_SHORT).show();
				}
			};
		}
		return mResponseHandler;
	}
}